package com.imooc.luckymoney.service;

import com.imooc.luckymoney.constant.Constant;
import com.imooc.luckymoney.repository.GroupCollection;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class ExcelService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(GroupCollection groupCollection) {
        mongoTemplate.save(groupCollection);
    }

    public GroupCollection findById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, GroupCollection.class);
    }

    public List<GroupCollection> findByIds(ArrayList<String> ids) {
        HashSet<String> setIds = new HashSet<>();
        setIds.add("001");
        setIds.add("003");
        setIds.add("002");
        Query query = new Query(Criteria.where("id").in(setIds)).with(Sort.by(Sort.Order.desc("id")));
        List<GroupCollection> groupCollections = mongoTemplate.find(query, GroupCollection.class);
        return groupCollections;
    }

    public void updateOne(GroupCollection groupCollection) {
        Query query = new Query(Criteria.where("id").is(groupCollection.getId()));
        Update modelName = new Update().set("modelName",groupCollection.getModelName());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, modelName, GroupCollection.class);
        System.out.println(updateResult);
    }

    public long updateOneObj(GroupCollection groupCollection) {
        Query query = new Query(Criteria.where("id").is(groupCollection.getId()));
        Update columns = new Update().set("columns", groupCollection.getColumns());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, columns, GroupCollection.class);
        long modifiedCount = updateResult.getModifiedCount();
        return modifiedCount;
    }

    public void updateBatch(GroupCollection groupCollection) {
        Query query = new Query(Criteria.where("id").is(groupCollection.getId()));
        Update modelName = new Update().set("modelName", "update");
        BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkOperations.BulkMode.ORDERED, Constant.GROUP_COLLECTION_NAME);
        bulkOperations.updateOne(query, modelName);
        BulkWriteResult execute = bulkOperations.execute();
        System.out.println(execute);
    }

    /**
     * create or update
     * @param collection
     */
    public void createOrUpdateById(GroupCollection collection) throws IllegalAccessException {
        Query query = new Query(Criteria.where("id").is(collection.getId()));
        Update update = new Update();
        Field[] fields = collection.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            update.set(field.getName(), field.get(collection));
        }

        mongoTemplate.upsert(query, update, GroupCollection.class);
    }

    public void batchCreateOrUpdateById(ArrayList<GroupCollection> collections) {

    }

    /**
     * remove by id
     * @param id
     */
    public long removeById(String id) {
        GroupCollection collection = mongoTemplate.findById(id, GroupCollection.class);
        if (collection != null) {
            DeleteResult remove = mongoTemplate.remove(collection);
            return remove.getDeletedCount();
        }

        return  0L;
    }
}
